package com.xijia.wuyouwallet.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

import com.alibaba.fastjson.JSONObject;
import com.xijia.wuyouwallet.R;
import com.xijia.wuyouwallet.adapter.DitchAdapter;
import com.xijia.wuyouwallet.api.MyCallBack;
import com.xijia.wuyouwallet.base.BaseActivity;
import com.xijia.wuyouwallet.bean.DitchData;
import com.xijia.wuyouwallet.utils.MyLogger;
import com.xijia.wuyouwallet.utils.UrlConfig;
import com.xijia.wuyouwallet.utils.XUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/11/7 0007.
 */

public class DitchActivity extends BaseActivity implements View.OnClickListener{

    private ListView ditch_lv;
    private ImageView ditch_backImg;

    private List<DitchData.DataBean.ListBean> mList;
    private DitchAdapter mAdapter;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ditch);
        initView();
    }

    private void initView(){
        ditch_lv = (ListView) findViewById(R.id.ditch_lv);
        ditch_backImg = (ImageView) findViewById(R.id.ditch_backImg);
        ditch_backImg.setOnClickListener(this);

        setListener();
    }

    private void setListener(){
        mList = new ArrayList<>();
        mAdapter = new DitchAdapter(mList,this);
        ditch_lv.setAdapter(mAdapter);

        ditch_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Bundle bundle = new Bundle();
                bundle.putString("ct_id",mList.get(i).getCt_id());
                jumpToPage(ReceiptMoneyActivity.class,bundle);
            }
        });

        requestData();
    }

    private void requestData(){
        showProgressDialog(true);
        XUtil.Post(UrlConfig.GET_DTICH_URL,null,new MyCallBack<String>(){

            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                dismissProgressDialog();
                MyLogger.wLog().e("获取渠道result="+result);
                DitchData data = JSONObject.parseObject(result,DitchData.class);
                if (data.getCode()==200){
                    for (DitchData.DataBean.ListBean listBean : data.getData().getList()){
                        mList.add(listBean);
                    }
                }
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                dismissProgressDialog();
                MyLogger.wLog().e("获取渠道onError="+ex.toString());
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ditch_backImg:
                finish();
                break;
        }
    }
}
